﻿<%@ Page Language="C#" %>

<script runat="server">
  [System.Web.Services.WebMethod]
  public static string Authenticate(string username, string password, bool rememberMe, string authType)
  {
    try
    {
      B2C_MALL_ORDER ws = new B2C_MALL_ORDER();
      ws.Url = ConfigurationManager.AppSettings["B2C_MALL_ORDER"].ToString();
      string jsonString =
        ws.B2C_MALL_DoLogin(ConfigurationManager.AppSettings["WebDeviceid"].ToString()
        , username, authType, password);
      //string jsonString = ws.B2C_MALL_DoLogin("aaaa", "3312010796", "1", "FFP2012");    
      //企業會員測試
      //string jsonString = ws.B2C_MALL_DoLogin("aaaa", "12345678", "2", "12345678");
      JObject restoredObject = JsonConvert.DeserializeObject<JObject>(jsonString);
      string Is_Valid = restoredObject["Is_Valid"].ToString();
      string msg = restoredObject["Msg"].ToString();

      if (Is_Valid == "Y")
      {
        string MemberID = username;
        string MemberType = "";
        //string MemberID = jo["MemberID"].ToString();
        if (!string.IsNullOrEmpty(MemberID))
        {
          //設定登入的使用者帳號至Cookie中
          //1.會員卡號
          //2.企業優惠帳號
          //3.身分證字號
          System.Web.Security.FormsAuthentication.SetAuthCookie(MemberID, rememberMe);

          string UserID = MemberID;
          string UserName = "親愛的會員";
          if (restoredObject["pUserInfo_Ary"].HasValues)
          {
            //因為User只有單筆資料，因此使用JObject解析
            JObject jo = (JObject)restoredObject["pUserInfo_Ary"];
            UserID = jo["UserID"].ToString();
            UserName = string.IsNullOrEmpty(jo["UserName"].ToString()) ? UserName : jo["UserName"].ToString();
            MemberType = jo["MemberType"].ToString();
          }
          //紀錄登入會員的資訊
          //1:會員
          //2:企業優惠
          //3:非會員
          HttpCookie authCookie = new HttpCookie("LoginInfo");
          authCookie.Values.Add("UserID", UserID);
          authCookie.Values.Add("MemberID", MemberID);
          authCookie.Values.Add("AuthType", authType); //記錄登入類型
          authCookie.Values.Add("MemberType", MemberType); //哩上加哩識別卡別使用
          authCookie.Values.Add("Password", password); //Call結帳ValidateOrderInfo使用
          authCookie.Values.Add("UserName", HttpContext.Current.Server.UrlEncode(UserName));
          HttpContext.Current.Response.Cookies.Add(authCookie);
        }
      }
      return jsonString;
    }
    catch (Exception)
    {
      return "-1";
    }
  }


</script>


